package pe.gob.foncodes.ssa.web.util;

import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author aespinoza
 */
public class Utilitarios {
 
    public static void mostrarMensajeInfo(String objectName, String summary, String details) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(objectName, new FacesMessage(FacesMessage.SEVERITY_INFO, summary, details ));
    }

    public static void mostrarMensajeError(String objectName, String summary, String details) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(objectName, new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, details ));
    }
    
    public static void mostrarMensajeAdvertencia(String objectName, String summary, String details) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(objectName, new FacesMessage(FacesMessage.SEVERITY_WARN, summary, details ));
    }
    
    public static void mostrarMensajeUsuario(String objectName, String summary, String details, Severity severity) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(objectName, new FacesMessage(severity, summary, details));
    }
    
    public static int obtenerDocumentoIdxDefecto() {
        ResourceBundle config = ResourceBundle.getBundle("config");
        return Integer.parseInt(config.getString("TipoDocumentoxDefecto").trim());
    }
    
    public static String getContextUrl(String strtipo) {
        String strresultado = "";
        try {
            Context initCtx = new InitialContext();
            Context myEnv = (Context) initCtx.lookup("java:comp/env");

            if (strtipo.equals("1")) {
                strresultado = (String) myEnv.lookup("strurlmail");
            } else if (strtipo.equals("2")) {
                strresultado = (String) myEnv.lookup("strusermail");
            } else if (strtipo.equals("3")) {
                strresultado = (String) myEnv.lookup("strpassmail");
            } else if (strtipo.equals("4")) {
                strresultado = (String) myEnv.lookup("strmailde");
            }
        } catch (NamingException e) {
            System.out.println("Error en Obtencion de Parmetros del WEB.XML (Parametros no configurados):" + e.getMessage());
        }
        return strresultado;
    }
    
    public static boolean tieneLetras(String cadena) {
        Pattern p = Pattern.compile("[A-Za-z]");
        Matcher m = p.matcher(cadena);
        if (m.find()) {
            return true;
        }
        return false;
    }
    
    public static boolean tieneNumeros(String cadena) {
        Pattern p = Pattern.compile("[0-9]");
        Matcher m = p.matcher(cadena);
        if (m.find()) {
            return true;
        }
        return false;
    }
}
